
(function () {
    'use strict';

    angular.module('app.maquinas')
        .controller('listarMaquinasCtrl', ['$scope', '$filter', 'MaquinasService', listarMaquinasCtrl])
        .controller('editarMaquinaCtrl', ['$scope', '$filter', '$stateParams', 'logger', 'MaquinasService', 'FileReaderFactory', editarMaquinaCtrl])
        .controller('registrarMaquinaCtrl', ['$scope', '$filter', 'logger', 'MaquinasService', 'FileReaderFactory','$location', registrarMaquinaCtrl]);

    function listarMaquinasCtrl ($scope, $filter, MaquinasService){
        var init;

        $scope.listOfItems = [];

        $scope.searchKeywords = '';

        $scope.filteredStores = [];

        $scope.row = '';

        $scope.select = function(page) {
            var end, start;
            start = (page - 1) * $scope.numPerPage;
            end = start + $scope.numPerPage;
            return $scope.currentPageItems = $scope.filteredStores.slice(start, end);
        };

        $scope.onFilterChange = function() {
            $scope.select(1);
            $scope.currentPage = 1;
            return $scope.row = '';
        };

        $scope.onNumPerPageChange = function() {
            $scope.select(1);
            return $scope.currentPage = 1;
        };

        $scope.onOrderChange = function() {
            $scope.select(1);
            return $scope.currentPage = 1;
        };

        $scope.search = function() {
            $scope.filteredStores = $filter('filter')($scope.listOfItems, $scope.searchKeywords);
            return $scope.onFilterChange();
        };

        $scope.order = function(rowName) {
            if ($scope.row === rowName) {
                return;
            }
            $scope.row = rowName;
            $scope.filteredStores = $filter('orderBy')($scope.listOfItems, rowName);
            return $scope.onOrderChange();
        };

        $scope.numPerPageOpt = [3, 5, 10, 20];

        $scope.numPerPage = $scope.numPerPageOpt[2];

        $scope.currentPage = 1;

        $scope.currentPageItems = [];

        init = function() {
            $scope.search();
            return $scope.select($scope.currentPage);
        };

        $scope.listarMaquinas = function() {
            MaquinasService.listar()
                .success(function (response) {
                    $scope.listOfItems = angular.fromJson(response);
                    init();
                }).error(function () {
                    console.log(arguments)
                });
        }

        $scope.eliminar= function(idMaquina) {
            MaquinasService.eliminar(idMaquina)
                .success(function (response) {
                    $scope.listarMaquinas();
                }).error(function () {
                    console.log(arguments);
                });
        }

        $scope.imagePreview = function(param){
            $scope.previewImage = 'data:image/jpeg;base64,' + param.item.ImagenBase64;
        }


        init();
    }

    function editarMaquinaCtrl ($scope, $filter, $stateParams, logger, MaquinasService, FileReaderFactory){
		$scope.maquina = {};

        $scope.getFile = function () {
            FileReaderFactory.readAsDataUrl($scope.file, $scope)
                          .then(function(result) {
                              $scope.maquina.ImagenBase64 = result;
                          });
        };
		//alert('el id del Maquina para editar es: ' + $stateParams.id);

        $scope.getMaquinaInfo = function(){
            MaquinasService.buscarPorId($stateParams.id).then(function(response) {
                    $scope.maquina = angular.fromJson(response.data);
                }, null);

            MaquinasService.listarTiposMaquinas()
                .success(function (response) {
                    $scope.tiposMaquinas = angular.fromJson(response);
                }).error(function () {
                    console.log(arguments)
                });

            /*    .success(function (response) {
                    $scope.maquina = angular.fromJson(response);
                }).error(function () {
                    console.log(arguments)
                });*/
        };
		
		$scope.guardarInfo = function () {
            $scope.maquina.NumeroMaquina = $scope.maquina.NumeroMaquina.toString();
            $scope.maquina.Id = $scope.maquina.Id.toString();
            $scope.maquina.IdTipoMaquina = $scope.maquina.IdTipoMaquina.toString();
            MaquinasService.update($scope.maquina)
                .success(function (response) {
                    logger.logSuccess($filter('translate')('GENERIC_AJAX_SUCCESS_SAVE'));
                }).error(function () {
                    console.log(arguments)
                });
		};


    }

    function registrarMaquinaCtrl($scope, $filter, logger, MaquinasService, FileReaderFactory, $location){
        $scope.maquina = {};

        $scope.getFile = function () {
            FileReaderFactory.readAsDataUrl($scope.file, $scope)
                          .then(function(result) {
                              $scope.maquina.fotoMaquina = result;
                          });
        };

        $scope.getMaquinaInfo = function(){
            MaquinasService.listarTiposMaquinas()
                .success(function (response) {
                    $scope.tiposMaquinas = angular.fromJson(response);
                }).error(function () {
                    console.log(arguments)
                });
        };

        $scope.guardarInfo = function(){
            var maq = angular.copy($scope.maquina);
            maq.estado = 'A';
            if (maq.fotoMaquina) {
                maq.fotoMaquina = maq.fotoMaquina.replace('data:image/jpeg;base64,','').replace('data:image/png;base64,','');
            } else {
                maq.fotoMaquina = "";
            }

            MaquinasService.crearMaquina(maq)
                .success(function (response) {
                    logger.logSuccess($filter('translate')('GENERIC_AJAX_SUCCESS_SAVE'));
                }).error(function () {
                    console.log(arguments);
                });
        };
        
    }




})();
